package com.simplegovern.authority.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simpleframe.persistence.hibernate.Page;
import com.simpleframe.persistence.hibernate.Searcher;
import com.simplegovern.authority.SysUser;
import com.simplegovern.authority.dao.UserDAO;

public class UserService {

	@Autowired
	private UserDAO ud;
	@Autowired
	private UserRoleService userRoleService;
	
	public SysUser getSysUser(long id){
		return ud.findById(id);
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteSysUser(Long[] ids){
		for (Long id : ids) {
			ud.delete(id);
			userRoleService.remove(id);
		}
		
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void createSysUser(SysUser sysUser,Long[] roleIds){
		ud.save(sysUser);
		userRoleService.add(roleIds, sysUser.getId());
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateSysUser(SysUser sysUser, Long[] roleIds){
		ud.update(sysUser);
		userRoleService.remove(sysUser.getId());
		userRoleService.add(roleIds, sysUser.getId());
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateSysUser(SysUser sysUser){
		ud.update(sysUser);
	}
	
	//以上自动生成
	
	public Page getSysUserPage(Searcher searcher){
		return ud.getSysUserPage(searcher);
	}
	
	public SysUser getSysUserByName(String name){
		return ud.getSysUserByName(name);
	}
}
